package com.shop.controller.admin;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.jf.core.BaseController;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.shop.bean.DpglBean;
import com.shop.bean.NzcpdlBean;
import com.shop.bean.NzcpxlBean;
import com.shop.bean.ProPicsBean;
import com.shop.bean.ProductBean;
import com.shop.utils.StringUtils;

public class ProductController extends BaseController{
	
	//获得所有的商品
	public void allproducts(){
		String select = "select tbp.*,tbcd.dlname,tbcx.xlname,tbdg.dpname ";
		String sqlExceptSelect = "from tblproduct tbp inner join tblnzcpdl tbcd on tbp.pdlcode=tbcd.dlcode"+
		" inner join tblnzcpxl tbcx on tbp.pxlcode=tbcx.xlcode inner join tbldpgl tbdg on tbp.pdpcode=tbdg.dpcode";
		Integer page = getParaToInt("page");
		if(page == null || "".equals(page)){
			page= 1;
		}
		Page<ProductBean> products = ProductBean.productDao.paginate(page, 55, select,sqlExceptSelect);
		setAttr("products", products);
		setAttr("productslist", products.getList());
		setAttr("dlname", "所有");
		render(WEB_INF_URL+"admin/files/listproducts.jsp");
	}
	
	//进入各类商品列表页面
	public void getProducts(){
		String dlcode = getPara("dlcode");
		String dlname = getPara("dlname");
		String select = "select tbp.*,tbcd.dlname,tbcx.xlname,tbdg.dpname";
		String sqlExceptSelect = "from tblproduct tbp inner join tblnzcpdl tbcd on tbp.pdlcode=tbcd.dlcode"+
		" inner join tblnzcpxl tbcx on tbp.pxlcode=tbcx.xlcode inner join tbldpgl tbdg on tbp.pdpcode=tbdg.dpcode"+" where tbp.pdlcode=?";
		Integer page = getParaToInt("page");
		if(page == null || "".equals(page)){
			page= 1;
		}
		Page<ProductBean> products = ProductBean.productDao.paginate(page, 55, select,sqlExceptSelect,dlcode);
		setAttr("products", products);
		setAttr("dlname", dlname);
		render(WEB_INF_URL+"admin/files/listproducts.jsp");
	}
	
	//激活选择的商品
	public void jhProductList(){
		final String pcodeList = getPara("pcodeList");
		boolean bool = Db.tx(new IAtom(){
			@Override
			public boolean run() throws SQLException {
				boolean result = true;
				String[] pcodeArr = pcodeList.split("@");
				for(String pcode: pcodeArr){
					result = ProductBean.productDao.findById(pcode).set("pstate", 1).update();
					if(!result){
						break;
					}
				}
				return result;
			}
		});
		if(bool){
			renderJson("message","true");
		}else{
			renderJson("message","false");
		}
	}
	
	//停用选择的商品
	public void delProductList(){
		final String pcodeList = getPara("pcodeList");
		boolean bool = Db.tx(new IAtom(){
			@Override
			public boolean run() throws SQLException {
				boolean result = true;
				String[] pcodeArr = pcodeList.split("@");
				for(String pcode: pcodeArr){
					result = ProductBean.productDao.findById(pcode).set("pstate", 0).update();
					if(!result){
						break;
					}
				}
				return result;
			}
		});
		if(bool){
			renderJson("message","true");
		}else{
			renderJson("message","false");
		}
	}
	
	//进入商品详细页面
	public void goToDetail(){
		String pcode = getPara("pcode");
		String sql_ = StringUtils.getSql("sql.find.findProductByCode", pcode);
		ProductBean product = ProductBean.productDao.findFirst(sql_);
		setAttr("product",product);
		render(WEB_INF_URL+"admin/files/productdetail.jsp");
	}
	
	//添加 商品页面
	public void addpropage(){
		setAttr("pcode",StringUtils.getTimeCode());
		String sql_dp = StringUtils.getSql("sql.find.findDpgl");
		List<DpglBean> listdp = DpglBean.dpglDao.find(sql_dp);
		setAttr("dpglList",listdp);
		//获取大类
		String sql_dl = StringUtils.getSql("sql.find.findCpdlList");
		List<NzcpdlBean> listdl = NzcpdlBean.cpdlDao.find(sql_dl);
		setAttr("cpdlList",listdl);
		//获取小类
		String sql_xl = StringUtils.getSql("sql.find.findCpxlList");
		List<NzcpxlBean> listxl = NzcpxlBean.cpxlDao.find(sql_xl);
		setAttr("cpxlList",listxl);
		
		render(WEB_INF_URL+"admin/files/addpropage.jsp");
	}
	// 添加商品
	public void doaddpro(){
		
		String pcode = StringUtils.getTimeCode();
		System.out.println("------------"+pcode);
		String picname = super.uploadFile("ppic", "productimg");
		String pname = getPara("pname");
		String pstate = getPara("pstate");
		String pdpSel = getPara("pdpSel");
		String cpdlSel = getPara("cpdlSel");// 大类
		String cpxlSel = getPara("cpxlSel");// 小类
 		String psj = getPara("psj");// 市场价格
 		String pjg = getPara("pxj");// 现价
 		String pkc = getPara("pkc");// 库存数量
 		String pdw = getPara("pdw");// 单位
		String pjf = getPara("pjf");// 积分
 		
		String pgljs = getPara("pgljs");
		String pqdesc =  getPara("pqdesc");
 		
		Record rd = new Record();
		rd.set("pcode", pcode)
			.set("pname",pname)
			.set("pstate",pstate) //1:在售 0:下架
			.set("pdpcode", pdpSel)
			.set("pdlcode", cpdlSel)
			.set("pxlcode", cpxlSel)
			.set("pfbtime", StringUtils.getFormatTime(new Date()))
			.set("pphoto",picname)
			.set("pscprice", psj)
			.set("phyprice", pjg)
			.set("psum", pkc)
			.set("putil", pdw)
			.set("khjf", pjf)
			.set("pjieshao", pgljs)
			.set("pqdesc", pqdesc);
		if(Db.save("tblproduct", rd)){
			allproducts();
		}else{
			setAttr("result", "商品添加失败。");
		}
		
	}
	
	// 上传图片 页面
	public void goToUpload(){
		String pcode= getPara("pcode");
		ProductBean pro = ProductBean.productDao.findById(pcode);
		setAttr("pro",pro);
		String sql = "select * from tblpics pc  where pc.pcode = ? order by pc.piccode DESC LIMIT 4";
		List<ProPicsBean> list = ProPicsBean.dao.find(sql,pcode);
		setAttr("pclist",list);
		render(WEB_INF_URL+"admin/files/propics.jsp");
	}
	//上传 图片
	public void douploadpropic(){
		String pcs = super.uploadFile("pcfile","productimg");
		String pcode = getPara("pcode");
		Record rd = new Record();
		rd.set("piccode", StringUtils.getTimeCode())
			.set("pcode", pcode)
			.set("picurl", pcs);
		if(Db.save("tblpics", rd)){
			goToUpload();
		}else{
			setAttr("result", "图片上传失败。");
		}
	}
	// 删除 商品图片
		public void removepropic(){
			String piccode = getPara("piccode");
			ProPicsBean pic = ProPicsBean.dao.findById(piccode);
			String picurl = pic.getStr("picurl");
			if(StringUtils.deleteFile(PathKit.getWebRootPath()+picurl) && pic.delete()){
				goToUpload();
			}else{
				setAttr("result", "图片删除失败。");
			}
		}
	
	//上传 图片 描述
		public void douploadpropicdesc(){
			String pcs = super.uploadFile("pcfile","productimg");
			String pcode = getPara("pcode");
			ProductBean pro = ProductBean.productDao.findById(pcode);
			if(pro.set("pdesc", pcs).update()){
				goToUpload();
			}else{
				setAttr("result", "图片上传失败。");
			}
		}
	
	// 删除 商品图片描述
	public void removepropicdesc(){
		String pcode = getPara("pcode");
		ProductBean pro = ProductBean.productDao.findById(pcode);
		String picurl = pro.getStr("pdesc");
		if(StringUtils.deleteFile(PathKit.getWebRootPath()+picurl)){
			goToUpload();
		}else{
			setAttr("result", "图片删除失败。");
		}
	}
}
